<?php

// *** Documentation
/*______________________________________________________________________________

  Bafflegate Site Metric Script
  Version 0.04
  
  Last Update: August 2007
  Author: Tom at klenwell@gmail.com
  
  SUMMARY
  track a website visitor
  
  A bafflegate is a turnstile.  The metaphor is one of presenting a ticket 
  (a baffleticket) at an admission gate and being counted
  
  NOTES  
  to test, call directly: http://localhost/BAFFLEGATE/version0/log.m04.php?baffleticket=direct&scan=YYMMDD
  
  To Do:
    config.inc.php -> constants
		
______________________________________________________________________________*/


// *** Pre-Script (core settings, includes)
/*____________________________________________________________________________*/

// ** Meta Variables
	
  // timer
  $_tx1 = microtime();
  
  // syntax declarations
  define('DS', DIRECTORY_SEPARATOR);
  
  // pre-script events
  if ( ob_get_level() == 0 ) ob_start();
  #session_start();
	

// ** Core Page Settings
$_DIR['root'] = '../';

	
// ** Includes
require_once('functions.m02.inc.php');
require_once('baffle.m01.class.php');
	
/*============================================================================*/



// *** Data (triggers, flags, HTML, and other variables)
/*____________________________________________________________________________*/

// ** Controller Flags

  // config settings
  $cue = 'baffleticket';

  // Triggers (Page Events)
  $_TRIGGER['cx_baffler'] = 0;
  $_TRIGGER['check_ticket'] = 0;
  $_TRIGGER['ticketless'] = 0;
  $_TRIGGER['log_visit'] = 0;     // -> $Baffler->ticket_is_valid
  $_TRIGGER['log_session'] = 0;
  $_TRIGGER['log_agent'] = 0;
  $_TRIGGER['exception'] = 0;     // -> $Baffler->is_exception


// ** Data
	

// ** Output


/*============================================================================*/



// *** Controllers (handles requests and sets event triggers)
/*____________________________________________________________________________*/

// Default
$_TRIGGER['cx_baffler'] = 1;

// GET Triggers
// this script will be called either from a javascript file or an img tag of 
// the page and thus should require some kind of GET value to trigger construction
// of the baffler object
if ( !empty($_GET[$cue]) )
{
  $_TRIGGER['check_ticket'] = 1;
}
else
{
  // can we assume that this is an error or an unwanted direct call to this file?
  $_TRIGGER['ticketless'] = 1;
}

// debug
if ( 0 )
{
  echo '<script type="text/javascript">alert("'. $_SERVER['PHP_SELF'] . ' called")</script>';
  die();
}

/*============================================================================*/



// *** Events (trigger-controlled events)
/*____________________________________________________________________________*/

// construct object
if ( $_TRIGGER['cx_baffler'] )
{
  $Baffler = new Baffle($debug=1);
  $Baffler->visitor_baffleticket = $_GET[$cue];
  $Baffler->get_client_data();
  $Baffler->select_account_data_by_domain($Baffler->visiting_domain);  
  
  // baffleticket, session
  $Baffler->baffleticket = $_GET[$cue];
}


// ticketless
if ( $_TRIGGER['ticketless'] )
{
  $Baffler->set_exception(BAFFLE_XC_TICKETLESS, 'no baffleticket');
}


// check ticket (validate bafflekey)
if ( $_TRIGGER['check_ticket'] )
{
  // visitor_baffleticket == account_baffleticket
  if ( $Baffler->visitor_baffleticket == $Baffler->account_baffleticket )
  {
    $Baffler->ticket_is_valid = 1;
  }
  else
  {
    $Baffler->set_exception(BAFFLE_XC_INVALIDTIX, 'wrong baffleticket (not matched for this domain)');
  }
  // visiting_domain == account_domain
  // this is asserted by the select query
  
  // check no-count list
  if ( $Baffler->ticket_is_valid )
  {
    // cue to log
    $_TRIGGER['log_visit'] = 1;
  
    // admin no-count cookie (log with a cancel code)
    if ( $_COOKIE['admin_'.$Baffler->account_baffleticket] == 1 )
    {
      $Baffler->cancel_code = BAFFLE_CC_ADMIN;
    }
    // blacklisted (no log!)
    elseif ( $Baffler->is_ip_blacklisted() )
    {
      $Baffler->set_exception(BAFFLE_XC_BLACKLIST, 'ip is blacklisted');
      $_TRIGGER['log_visit'] = 0;
    }
    // other cases?
  }
  
  // new visitor or existing session
  if ( $_TRIGGER['log_visit'] )
  {
    // new visitor or existing session
    if ( $Baffler->is_new_visitor() ) // if not, should set session_uid
    {
      $_TRIGGER['log_session'] = 1;
    }
    else  // session exists, so we need not log, just make sure session uid is in log
    {
      $_DATA['session_uid'] = $Baffler->session_uid;
    }
  }
}


// log session
if ( $_TRIGGER['log_session'] ) 
{
  $_DATA['session_uid'] = $Baffler->insert_session();
  $Baffler->set_session_trackers($_DATA['session_uid']); 
}

// log visit
if ( $_TRIGGER['log_visit'] )
{
  $Baffler->insert_hit($_DATA['session_uid']);
}


// exception
if ( $Baffler->has_exception() )
{
  $Baffler->insert_exception();
}


// debug
if ( $Baffler->debug )
{
  $Baffler->write_file(print_r($Baffler,1), 'Baffle.debug.object_dump.' . date('Ymdhi') . '.txt');
  $Baffler->debug_dump();
}

/*============================================================================*/


// *** Output (handles requests and sets event triggers)
/*____________________________________________________________________________*/


/*============================================================================*/



// *** Post-Script (close HTML, flush buffer, and other backend operations)
/*____________________________________________________________________________*/

// ** Flush Buffer
while (ob_get_level() > 0) { ob_end_flush(); }
flush();
$_HTML['flush'] = "<!-- buffer flushed at " . date('r') . " -->";

// ** Page Timer
$_tx2 = microtime();
$_tx3 = number_format(((substr($_tx2,0,9)) + (substr($_tx2,-10)) - (substr($_tx1,0,9)) - (substr($_tx1,-10))),4); 
$_HTML['timer'] = "\n<!-- script [id: {$_PAGE['id']}] generated in $_tx3 s -->";

/*============================================================================*/
// * That's All Folks!
?>
